//
//  BMAResponse.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2016/11/2.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "BMAResponse.h"

@implementation BMAResponse

- (instancetype)initWithDictionary:(id)response error:(NSError *)error{

    if (self=[super init]) {
        
        if (error) {
            self.error_code = @"-1002";
            self.error_msg  = error.description;
            self.success    = NO;
            self.result     = nil;
        }else{
            if (response) {
                if ([response isKindOfClass:[NSData class]]) {
                    NSError *error=nil;
                    NSDictionary *dic= [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableContainers error: &error];
                    if (error) {
                        self.error_code = @"-1000";
                        self.error_msg  = @"数据转化为json失败";
                        self.success    = NO;
                        self.result     = response;
                    }else{
                        self.error_code = [dic objectForKey:@"error_code"];
                        self.error_msg  = nil;
                        self.success    = YES;
                        self.result     = dic;
                    }
                }else if ([response isKindOfClass:[NSDictionary class]]){
                    self.error_code = [response objectForKey:@"error_code"];
                    self.error_msg  = nil;
                    self.success    = YES;
                    self.result     = response;
                }else{
                    self.error_code = @"-1001";
                    self.error_msg  = @"未知对象";
                    self.success    = NO;
                    self.result     = response;
                }
                
            }else{
                self.error_code = @"-1002";
                self.error_msg  = @"返回数据为nil";
                self.success    = NO;
                self.result     = nil;
            }
        }
    }
    return self;
}

@end
